﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using online_magazine1.Models;

namespace online_magazine1.Controllers
{ 
    [Authorize]
    public class ArticleManagerController : Controller
    {
        private MagazineEntities db = new MagazineEntities();
        private static string deleteFileUrl = "";

        //
        // GET: /ArticleManager/

        public ViewResult Index()
        {
            string userName = User.Identity.Name;
            var articles = db.Articles.Include(a => a.Theme).Where(a => a.AuthorId == userName);
            return View(articles.ToList());
        }

        //
        // GET: /ArticleManager/Details/5


        //
        // GET: /ArticleManager/Create

        public ActionResult Create()
        {
            ViewBag.ThemeId = new SelectList(db.Themes, "ThemeId", "ThemeName");
            return View();
        } 

        //
        // POST: /ArticleManager/Create

        [HttpPost]
        public ActionResult Create(Article article, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                    if (file != null && file.ContentType == "application/msword")
                    {
                        string userName = User.Identity.Name;
                        string path = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles/";
                        string filename = Path.GetFileName(file.FileName);
                        if (filename != null) file.SaveAs(Path.Combine(path, filename));
                        article.FileUrl = Path.Combine(path, filename);
                        article.DateCreated = DateTime.Now;
                        article.AuthorId = userName;
                        db.Articles.Add(article);
                        db.SaveChanges();
                        return RedirectToAction("Index");
                    }
                  
            }

            ViewBag.ThemeId = new SelectList(db.Themes, "ThemeId", "ThemeName", article.ThemeId);
            return View(article);
        }
        
        //
        // GET: /ArticleManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            Article article = db.Articles.Find(id);
            deleteFileUrl = String.Copy(article.FileUrl);
            ViewBag.ThemeId = new SelectList(db.Themes, "ThemeId", "ThemeName", article.ThemeId);
            return View(article);
        }

        //
        // POST: /ArticleManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Article article, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null && file.ContentType == "application/msword")
                {
                    //string fileurl = article.FileUrl;
                    System.IO.File.Delete(deleteFileUrl);
                    string path = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles/";
                    string filename = Path.GetFileName(file.FileName);
                    if (filename != null) file.SaveAs(Path.Combine(path, filename));
                    article.FileUrl = Path.Combine(path, filename);
                    article.DateCreated = DateTime.Now;
                    
                }
                db.Entry(article).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ThemeId = new SelectList(db.Themes, "ThemeId", "ThemeName", article.ThemeId);
            return View(article);
        }

        //
        // GET: /ArticleManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            Article article = db.Articles.Find(id);
            return View(article);
        }

        //
        // POST: /ArticleManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Article article = db.Articles.Find(id);
            if (article.FileUrl != null && article.FileUrl != "")
                System.IO.File.Delete(article.FileUrl);
            db.Articles.Remove(article);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}